#!/usr/bin/python3
"""
Construct an FAT file-system via mkfs.fat(8)

Construct a FAT file-system with the given options at the device
specified via `device`.

Buildhost commands used: `mkfs.fat`.
"""

import subprocess
import sys


import osbuild.api


SCHEMA_2 = r"""
"devices": {
  "type": "object",
  "additionalProperties": false,
  "required": ["device"],
  "properties": {
    "device": {
      "type": "object",
      "additionalProperties": true
    }
  }
},
"options": {
  "additionalProperties": false,
  "required": ["volid"],
  "properties": {
    "volid": {
      "description": "Volume identifier",
      "type": "string"
    },
    "label": {
      "description": "Label for the file system",
      "type": "string",
      "maxLength": 11
    },
    "fat-size": {
      "description": "Label for the file system",
        "type": "integer",
        "enum": [12, 16, 32]
    }
  }
}
"""


def main(devices, options):
    device = devices["device"]["path"]

    volid = options["volid"]
    label = options.get("label")
    fatsize = options.get("fat-size")
    opts = []

    if label:
        opts = ["-n", label]

    if fatsize:
        opts = ["-F", str(fatsize)]

    subprocess.run(["mkfs.fat", "-i", volid] + opts + [device],
                   encoding='utf-8', check=True)


if __name__ == '__main__':
    args = osbuild.api.arguments()
    ret = main(args["devices"], args["options"])
    sys.exit(ret)
